package com.xs.ahoi.hcxs.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.utils.StringUtils;

/**
 * 通用的提示信息弹窗页面
 */
public class DialogCommonTips extends BaseDialog {

    @BindView(R.id.tvInfo)
    public TextView tvInfo;
    @BindView(R.id.dialogTitle)
    public TextView dialogTitle;
    private CommonTipsListener listener;

    // 标题
    private String title;
    // 内容
    private String content;

    public DialogCommonTips(Context context) {
        super(context, R.style.common_dialog);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_common_tips;
    }

    @Override
    protected int showPosition() {
        return Gravity.CENTER;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (StringUtils.isNotEmpty(content)) {
            String text = "缩进" + content;
            SpannableStringBuilder span = new SpannableStringBuilder(text);
            span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvInfo.setText(span);
        }
        if (StringUtils.isNotEmpty(title)) {
            dialogTitle.setText(title);
        }
    }

    @OnClick({R.id.tvDialogOk, R.id.tvDialogCancel})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvDialogOk:
                listener.ok();
                super.dismiss();
                break;
            case R.id.tvDialogCancel:
                super.dismiss();
                break;
        }
    }

    public void show(String title, String content) {
        this.title = title;
        this.content = content;
        show();
    }

    public void show(String content) {
        show(null, content);
    }

    public void setListener(CommonTipsListener listener) {
        this.listener = listener;
    }

    public interface CommonTipsListener {
        void ok();
    }
}
